package com.controller;

import com.VO.TalkRecordsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dao.LoginDao;
import com.entity.ResponseMessage;
import com.entity.TalkKiara;
import com.entity.TalkRecords;
import com.service.TalkKiaraService;
import com.service.TalkRecordsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;

/**
 * @author WZ
 * @version 1.0
 * @date 2021/10/1 14:55
 */
@RestController
@CrossOrigin
public class TalkRecordsController {
  @Autowired
	private TalkRecordsService talkRecordsService;
  @Autowired
  private TalkKiaraService talkKiaraService;
   @Autowired
   private LoginDao loginDao;
  @RequestMapping("sendMessage")
  @CrossOrigin
	public ResponseMessage sendMessage(@RequestBody TalkRecords talkRecords, @RequestHeader() String token){
      //先根据token获取当前登录的userId

	  Integer userId1=LoginController.tokenLists.stream().filter(s->token.equals(s.getToken())).collect(Collectors.toList()).get(0).getUserId();
       TalkRecords talkRecords1=new TalkRecords();
	  BeanUtils.copyProperties(talkRecords,talkRecords1);
	  talkRecords1.setUserId(userId1);
	  talkRecords1.setPostTime(new Date());
	  talkRecords1.setKiaraId(loginDao.findUserKiaraId(userId1));


	  boolean save = talkRecordsService.save(talkRecords1);


	  return new ResponseMessage(ResponseMessage.SUCCESS,"成功",save);
  }
  //获取聊天记录
  @RequestMapping("listRecords")
  @CrossOrigin
	public ResponseMessage findRecordsList(Long current,Long size){

	  IPage<TalkRecords> page=new Page<>(current,size);
	  IPage<TalkRecords> talkRecordsIPage=talkRecordsService.page(page,new LambdaQueryWrapper<TalkRecords>().orderByDesc(TalkRecords::getPostTime));
	  List<TalkRecordsVO> talkRecordsVOList =talkRecordsIPage.getRecords().stream().map(s->{
         TalkRecordsVO talkRecordsVO=new TalkRecordsVO();
         talkRecordsVO.setId(s.getId());
         talkRecordsVO.setContent(s.getContent());
         talkRecordsVO.setStatus(s.getStatus());
         talkRecordsVO.setKiaraId(s.getKiaraId());
         talkRecordsVO.setUserId(s.getUserId());
		  TalkKiara byId = talkKiaraService.getById(s.getKiaraId());
		  talkRecordsVO.setKiaraName(byId.getName());
		  talkRecordsVO.setKiaraImgUrl(byId.getImgurl());
		  SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		 talkRecordsVO.setPostTime(simpleDateFormat.format(s.getPostTime()));
		  return talkRecordsVO;
	  }).collect(Collectors.toList());
	  IPage<TalkRecordsVO> pageVO=new Page<>();
	  pageVO.setRecords(talkRecordsVOList);
	  pageVO.setTotal(talkRecordsIPage.getTotal());
	  pageVO.setCurrent(current);
	  pageVO.setSize(size);
//       List<TalkRecords> sss=talkRecordsIPage.getRecords();
//       sss.remove(0);
//	  talkRecordsIPage.setRecords(sss);
//	  talkRecordsIPage.setTotal(sss.size());

	  return  new ResponseMessage(ResponseMessage.SUCCESS,"成功",pageVO);
  }
}
